package net.lzzy.practices.wed.course;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstants;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Chapter;
import net.lzzy.practices.services.ChapterService;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.wed.models.ApiResult;
import net.lzzy.practices.wed.models.ChapterInfo;
import net.lzzy.practices.wed.models.ChapterKeyInfo;
import net.lzzy.practices.wed.models.VmChapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Api(tags = "章节相关业务")
@CrossOrigin
@RestController
@RequestMapping(ApiConstants.ROTE_CHAPTER_ROOT)
public class ChapterApiController {
    private final ChapterService service;

    @Autowired
    public ChapterApiController(ChapterService service){
        this.service = service;
    }

    @ApiOperation(value = "获取某教师本人上传的某课程的所有章节", notes = "<b>参数列表：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.courseId(UUID)<br/>")
    @GetMapping(ApiConstants.ROTE_CHAPTER_TEACHER)
    public ApiResult getTeachersChapters(@RequestParam String token,@RequestParam(name = "id") UUID courseId){
        List<Chapter> chapters = service.getTeachersChapters(courseId,token);
        return transform(chapters);
    }

    private ApiResult transform(List<Chapter> chapters){
        if (chapters == null){
            return new ApiResult(false, Messages.WRONG_ID + "或" + Messages.NO_PERMISSION,null);
        }
        List<VmChapter> vmChapters = new ArrayList<>();
        chapters.forEach(chapter -> vmChapters.add(new VmChapter(chapter)));
        return new ApiResult(true, vmChapters.size() + "",vmChapters);
    }

    @ApiOperation(value = "获取某学生注册的某课程的所有章节", notes = "<b>参数列表：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.courseId(UUID)<br/>")
    @GetMapping(ApiConstants.ROTE_CHAPTER_STUDENT)
    public ApiResult getStudentsChapter(@RequestParam String token,@RequestParam(name = "id") UUID courseId){
        List<Chapter> chapters = service.getStudentsChapters(courseId,token);
        return transform(chapters);
    }

    private ApiResult addOrUpdate(UUID id, ChapterInfo info){
        String token = info.getToken();
        String name = info.getName();
        String desc = info.getDesc();
        String strCourseId = info.getCourseId();
        boolean open = info.isOpen();
        if (StringUtils.isEmpty(token) ||
                StringUtils.isEmpty(name) ||
                StringUtils.isEmpty(desc) ||
                StringUtils.isEmpty(strCourseId)){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        boolean create = id == null;
        UUID courseId = UUID.fromString(strCourseId);
        Chapter chapter = new Chapter();
        chapter.setOpen(open);
        chapter.setName(name);
        chapter.setDescription(desc);
        VmChapter vmChapter;
        if (create){
            vmChapter = new VmChapter(service.add(courseId,chapter,token));
        }else {
            vmChapter = new VmChapter(service.update(id,chapter,token));
        }
        return new ApiResult(true, Messages.SUCCESS.toString(), vmChapter);
    }

    @ApiOperation(value = "某教师添加章节到自己的某一课程", notes = "<b>参数列表：<b/><br>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.courseId(UUID)<br/>" +
            "&nbsp;&nbsp;&nbsp;&nbsp;3.chapter(String)<br/>")
    @PostMapping(ApiConstants.ROTE_CHAPTER_ADD)
    public ApiResult add(@RequestBody ChapterInfo info){
        return addOrUpdate(null,info);
    }

    @ApiOperation(value = "某教师更新自己的某一课程的某一章节", notes = "<b>参数列表：<b/><br>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.id(UUID)<br/>" +
            "&nbsp;&nbsp;&nbsp;&nbsp;3.chapter(String)<br/>")
    @PostMapping(ApiConstants.ROTE_CHAPTER_UPDATE)
    public ApiResult update(@PathVariable UUID id,@RequestBody ChapterInfo info){
        return addOrUpdate(null,info);
    }

    @ApiOperation(value = "某教师开放或关闭某一章节", notes = "<b>参数列表：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;strChapterId(UUID)<br/>")
    @PostMapping(ApiConstants.ROTE_CHAPTER_OPEN_CLOSE)
    public ApiResult openCloseChapter(@RequestBody ChapterInfo info){
        String token = info.getToken();
        String strCourseId = info.getCourseId();
        boolean open = info.isOpen();
        if (StringUtils.isEmpty(token) ||
                StringUtils.isEmpty(strCourseId)){
            return new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
        }
        Chapter chapter = service.openCloseChapter(UUID.fromString(strCourseId),open,token);
        if (chapter == null){
            return new ApiResult(false,Messages.WRONG_ID + "或" + Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(),new VmChapter(chapter));
    }

    @ApiOperation(value = "某教师删除自己某一课程某一章节", notes = "<b>参数列表：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;strChapterId(UUID)<br/>")
    @DeleteMapping(ApiConstants.ROTE_CHAPTER_REMOVE)
    public ApiResult delete(@RequestBody ChapterKeyInfo info){
        String token = info.getToken();
        String strCourseId = info.getChapterId();
        if (StringUtils.isEmpty(token) ||
                StringUtils.isEmpty(strCourseId)){
            return new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
        }
        Messages msg = service.remove(UUID.fromString(strCourseId),token);
        return new ApiResult(msg == Messages.SUCCESS,msg.toString(),null);
    }
}
